package willevaluate.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.example.xuegengwang.xuegengwang.R;
import com.google.gson.Gson;

import org.json.JSONException;
import org.json.JSONObject;
import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.ViewInject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import common.BaseActivity;
import common.Constant;
import https.InterNetController;
import utils.MyLogUtils;
import view.MyConfirmDialog;
import willevaluate.adapter.ExpertEvaluateCompleteCollegeAdapter;
import willevaluate.bean.ExpertEvaluateBean;
import willevaluate.bean.ExpertEvaluateCollegeBean;
import willevaluate.bean.ExpertEvaluateCollegeDetailBean;

/**
 * Created by Administrator on 2017/5/16.
 * 3800/5800/8800块填学校志愿
 */

@ContentView(R.layout.expert_evaluate_complete_info)
public class ExpertEvaluateCompleteCollege extends BaseActivity implements View.OnClickListener {
    @ViewInject(R.id.common_back)
    private ImageView common_back;
    @ViewInject(R.id.common_title)
    private TextView common_title;
    @ViewInject(R.id.expert_evaluate_complete_info_lv)
    private ListView expert_evaluate_complete_info_lv;
    @ViewInject(R.id.expert_evaluate_complete_info_commit)
    private Button expert_evaluate_complete_info_commit;
    private ArrayList<ExpertEvaluateCollegeBean> colleges;
    private ExpertEvaluateCompleteCollegeAdapter expertEvaluateCompleteCollegeAdapter;
    private View footerView;
    private static final int REQUEST_CODE_ADD = 1;//添加
    private static final int REQUEST_CODE_EDIT = 2;//修改
    private String orderid;
    private static final String TAG = "ExpertEvaluateCompleteC";

    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case Constant.ERROR_CODE_SUCCESS://成功返回
                    try {
                        JSONObject jsonObject = new JSONObject(String.valueOf(msg.obj));
                        int err_code = jsonObject.getInt("err_code");
                        if (0 == err_code) {
                            ActivityController.finishActivity();
                            //回到志愿填报首页
                            Intent i = new Intent(ExpertEvaluateCompleteCollege.this, ExpertEvaluate.class);
                            HashMap<String, String> hashMap = new HashMap<>();
                            String status = jsonObject.getString("status");
                            String update_time = jsonObject.getString("update_time");
                            String notice = jsonObject.getString("notice");
                            hashMap.put("orderid", orderid);
                            hashMap.put("status", status);
                            hashMap.put("notice", notice);
                            hashMap.put("update_time", update_time);
                            i.putExtra("params", hashMap);
                            startActivity(i);
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                    break;
                default:
                    break;
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ActivityController.addActivity(this);
        orderid = getIntent().getStringExtra("orderid");
        colleges = new ArrayList<>();//要在initSchoolData之前初始化
        initSchoolData();
        initView();
        upDateListView();
    }


    private void initSchoolData() {
        //如果有传入高校列表
        ExpertEvaluateBean expertEvaluateBean = (ExpertEvaluateBean) getIntent().getSerializableExtra("expertEvaluateBean");
        if (expertEvaluateBean != null) {
            List<HashMap<String, List<String>>> collegesDetails = expertEvaluateBean.getCollegesDetails();
            if (collegesDetails != null) {
                for (int i = 0; i < collegesDetails.size(); i++) {
                    ExpertEvaluateCollegeBean expertEvaluateCollegeBean = new ExpertEvaluateCollegeBean();//每个学校
                    List<ExpertEvaluateCollegeDetailBean> majors1 = new ArrayList<>();//每个学校的专业数组
                    HashMap<String, List<String>> hashMap = collegesDetails.get(i);
                    Set<String> keys = hashMap.keySet();// 得到全部的key
                    Iterator<String> iterator = keys.iterator();
                    if (iterator.hasNext()) {
                        Object o = iterator.next();
                        String schoolName = o.toString();
                        expertEvaluateCollegeBean.setCollegeName(schoolName);//设置学校名
                        List<String> majors = hashMap.get(schoolName);
                        if (majors != null) {
                            for (int j = 0; j < majors.size(); j++) {
                                ExpertEvaluateCollegeDetailBean expertEvaluateCollegeDetailBean = new ExpertEvaluateCollegeDetailBean();//每一个专业
                                expertEvaluateCollegeDetailBean.setName(majors.get(j));
                                expertEvaluateCollegeDetailBean.setPosition(j);
                                majors1.add(expertEvaluateCollegeDetailBean);
                            }
                            expertEvaluateCollegeBean.setMajors(majors1);//设置对应专业名
                        }
                    }
                    colleges.add(expertEvaluateCollegeBean);
                }
            }
        }
    }

    private void initView() {
        common_back.setOnClickListener(this);
        common_title.setText("模拟填报志愿");
        expert_evaluate_complete_info_commit.setOnClickListener(this);
        expertEvaluateCompleteCollegeAdapter = new ExpertEvaluateCompleteCollegeAdapter(this, colleges, new ExpertEvaluateCompleteCollegeAdapter.MyOnClick() {
            @Override
            public void OnEditListener(int position) {
                Intent i = new Intent(ExpertEvaluateCompleteCollege.this, ExpertEvaluateCollegeDetail.class);
                i.putExtra("bean", colleges.get(position));
                i.putExtra("position", position);
                ExpertEvaluateCompleteCollege.this.startActivityForResult(i, REQUEST_CODE_EDIT);
                ExpertEvaluateCompleteCollege.this.overridePendingTransition(R.anim.zoomin, 0);
            }
        }, new ExpertEvaluateCompleteCollegeAdapter.OnDeleteListener() {
            @Override
            public void onDeleteListener(final int position) {
                MyConfirmDialog myConfirmDialog = new MyConfirmDialog(ExpertEvaluateCompleteCollege.this, "确认删除当前志愿?", "确认", "取消");
                myConfirmDialog.show();
                myConfirmDialog.setConfirmListener(new MyConfirmDialog.ConfirmListener() {
                    @Override
                    public void confirm() {
                        colleges.remove(position);
                        expertEvaluateCompleteCollegeAdapter.notifyDataSetChanged();
                        upDateListView();
                    }
                });
            }
        });
        footerView = LayoutInflater.from(this).inflate(R.layout.expert_evaluate_complete_college_footerview, null);
        footerView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent i = new Intent(ExpertEvaluateCompleteCollege.this, ExpertEvaluateCollegeDetail.class);
                i.putExtra("position", colleges.size());
                ExpertEvaluateCompleteCollege.this.startActivityForResult(i, REQUEST_CODE_ADD);
                ExpertEvaluateCompleteCollege.this.overridePendingTransition(R.anim.zoomin, 0);
            }
        });
        expert_evaluate_complete_info_lv.setAdapter(expertEvaluateCompleteCollegeAdapter);
    }

    private void upDateListView() {
        if (colleges.size() < 7) {
            if (expert_evaluate_complete_info_lv.getFooterViewsCount() < 1) {
                expert_evaluate_complete_info_lv.addFooterView(footerView);
                expert_evaluate_complete_info_lv.setFooterDividersEnabled(false);
            }
        } else {
            if (expert_evaluate_complete_info_lv.getFooterViewsCount() > 0) {
                expert_evaluate_complete_info_lv.removeFooterView(footerView);
            }
        }
        expertEvaluateCompleteCollegeAdapter.notifyDataSetChanged();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.common_back:
                this.finish();
                break;
            case R.id.expert_evaluate_complete_info_commit:
                commit();
                break;
            default:
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode != RESULT_CANCELED) {
            int position = data.getIntExtra("position", -1);
            ExpertEvaluateCollegeBean expertEvaluateCollegeBean = (ExpertEvaluateCollegeBean) data.getSerializableExtra("bean");
            if (expertEvaluateCollegeBean != null) {
                if (position != -1) {
                    switch (requestCode) {
                        case REQUEST_CODE_ADD:
                            colleges.add(expertEvaluateCollegeBean);
                            break;
                        case REQUEST_CODE_EDIT:
                            colleges.get(position).setCollegeName(expertEvaluateCollegeBean.getCollegeName());
                            colleges.get(position).setMajors(expertEvaluateCollegeBean.getMajors());
                            break;
                        default:
                            break;
                    }
                    expertEvaluateCompleteCollegeAdapter.notifyDataSetChanged();
                    upDateListView();
                }
            }
        }
    }

    private void commit() {
        Gson gson = new Gson();
        HashMap<String, List<String>> hashMap;
        ArrayList<HashMap> arrayList = new ArrayList<>();
        HashMap<String, String> hashMap1 = new HashMap<>();
        for (ExpertEvaluateCollegeBean expertEvaluateCollegeBean : colleges) {
            List<ExpertEvaluateCollegeDetailBean> majors = expertEvaluateCollegeBean.getMajors();
            List<String> majors1 = new ArrayList<>();
            for (int i = 0; i < majors.size(); i++) {
                majors1.add(majors.get(i).getName());
            }
            hashMap = new HashMap<>();
            hashMap.put(expertEvaluateCollegeBean.getCollegeName(), majors1);
            arrayList.add(hashMap);
        }
        String s = gson.toJson(arrayList);
        hashMap1.put("orderid", orderid);
        hashMap1.put("collegeList", s);
        MyLogUtils.e(TAG, "orderid+" + orderid);
        new InterNetController(this, Constant.SUBMITCOLLEGE, handler, hashMap1, 0);
    }
}
